package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragment;
import com.zzptc.zzsgz.bdsjws.services.MyDownLoadService;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class UpdateVersionFragment extends BaseFragment {


    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.ok)
    Button ok;
    @BindView(R.id.no)
    Button no;
    Unbinder unbinder;
    Unbinder unbinder1;
    VersionInfo versionInfo;
    @Override
    public void intnDateView(View view) {
        Bundle bundle = getArguments();
        VersionInfo versionInfo = (VersionInfo) bundle.getSerializable("VersionInfo");
        if (versionInfo != null) {
            tvUpdate.setText(versionInfo.getInfo());
            no.setVisibility(View.VISIBLE);
        } else {
            tvUpdate.setText(getMsg());
        }
    }

    @Override
    public int getContentXml() {
        return R.layout.update_version;
    }

    public String getMsg() {
        Bundle bundle = getArguments();
        String msg = bundle.getString("msg");
        return msg;
    }


    public static UpdateVersionFragment getUpdateVersionFragment(String msg) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("msg", msg);
        updateVersionFragment.setArguments(bundle);
        return updateVersionFragment;
    }


    public static UpdateVersionFragment getUpdateVersionFragment(VersionInfo versionInfo) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("VersionInfo", versionInfo);
        updateVersionFragment.setArguments(bundle);
        return updateVersionFragment;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(unbinder!=null)
        {
            unbinder.unbind();
        }

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.ok, R.id.no})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ok:
                Bundle bundle = getArguments();
                VersionInfo versionInfo = (VersionInfo) bundle.getSerializable("VersionInfo");
                Intent intent = new Intent(new Intent(MyApplication.getContext(),MyDownLoadService.class));
                intent.putExtra("apkurl" , versionInfo.getApkUrl());

                MyApplication.getContext().startService(intent);

                dismiss();
                break;
            case R.id.no:
                dismiss();
                break;
        }
    }
}
